package com.yanqu.road.server.cmd.cross3.xrank;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.rank.eRankInfo;
import com.yanqu.road.pb.cross3.activity.XRankProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.Cross3Protocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_XRANK_GET_USER_RANK_LIST, desc = "个人榜单")
public class GetXRankUserRankListCmd extends AbstractCommand {

    private int activityId;
    private int conditionType;
    private int pageNo;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        XRankProto.GetXUserRankListReqMsg reqMsg = XRankProto.GetXUserRankListReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        conditionType = reqMsg.getConditionType();
        if (reqMsg.hasPageNo()) {
            pageNo = reqMsg.getPageNo();
        }
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(activityId);
        if (!ActivityMgr.activityInShowTime(activityInfo)) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }

        eRankInfo rankInfo = eRankInfo.getERankInfo(activityInfo.getType(), activityInfo.getChildType(), conditionType);
        if (rankInfo == null) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND; // 随便弄的错误码
            return;
        }


        // 到跨服拿个人榜
        XRankProto.GetXUserRankListReqMsg.Builder builder = XRankProto.GetXUserRankListReqMsg.newBuilder();
        builder.setActivityId(activityId);
        builder.setConditionType(conditionType);
        player.sendPacket(Cross3Protocol.C3_XRANK_GET_USER_RANK_LIST, builder);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            XRankProto.GetXUserRankListRespMsg.Builder builder = XRankProto.GetXUserRankListRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_XRANK_GET_USER_RANK_LIST, builder);
        }
    }
}
